using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using FarPoint.Win.Spread;
using FarPoint.Win;

namespace Spread.ConditionalFormat
{
    public partial class Introduction : BaseSampleControl
    {
        private Color _bclrHeader = Color.DarkGreen;
        private Color _fclrHeader = Color.White;
        private Font _fntHeader = new Font("Segoi UI", 10, FontStyle.Bold);
        private ComplexBorder _bdrLine = new ComplexBorder(new ComplexBorderSide(ComplexBorderSideStyle.Dashed), new ComplexBorderSide(ComplexBorderSideStyle.Dashed), new ComplexBorderSide(ComplexBorderSideStyle.Dashed), new ComplexBorderSide(ComplexBorderSideStyle.Dashed));

        private UnaryComparisonConditionalFormattingRule _unary;
        private BetweenValuesConditionalFormattingRule _betweenFileringl;
        private TwoColorScaleConditionalFormattingRule _twoColorScale;
        private ThreeColorScaleConditionalFormattingRule _threeColorScale;
        private TimePeriodConditionalFormattingRule _timePeriiodRule;
        private TextConditionalFormattingRule _textFormattingRule;
        private DatabarConditionalFormattingRule _dataBarRule;
        private IconSetConditionalFormattingRule _iconSetRule;
        public Introduction()
        {
            InitializeComponent();

            _sheetView.SetRowHeight(-1, 25);
            //Header
            SetTitle(1, 1, 2, 13, "Conditional Format Samples");
            _sheetView.Cells[1, 1].HorizontalAlignment = CellHorizontalAlignment.Center;
            _sheetView.Cells[1, 1].VerticalAlignment = CellVerticalAlignment.Center;
            _sheetView.Cells[1, 1].Font = new Font("Segoi UI", 14, FontStyle.Bold);
            
            //Conditional Formats
            //Table 1
            _unary = new UnaryComparisonConditionalFormattingRule(UnaryComparisonOperator.GreaterThanOrEqualTo, 100, false);
            _unary.BackColor = Color.FromArgb(204, 255, 204);
            SetTableAndFormats(4, 1, 4, 6, "Displays green background if cell value > 100:", _unary);
            
            //Table 2
            _betweenFileringl = new BetweenValuesConditionalFormattingRule(false, 60, false, 120, false);
            _betweenFileringl.BackColor = Color.FromArgb(255, 204, 204);
            SetTableAndFormats(4, 8, 4, 6, "Displays red background if value >= 60 and value <= 120:", _betweenFileringl);
            
            //Table 3
            _twoColorScale = new TwoColorScaleConditionalFormattingRule(Color.FromArgb(255, 153, 153), Color.FromArgb(153, 153, 255), true);
            SetTableAndFormats(10, 1, 4, 6, "Displays two color gradient representing cell value:", _twoColorScale);
            
            //Table 4
            _threeColorScale = new ThreeColorScaleConditionalFormattingRule(Color.FromArgb(255, 153, 153), Color.FromArgb(153, 255, 153), Color.FromArgb(153, 153, 255));
            SetTableAndFormats(10, 8, 4, 6, "Displays three color gradient representing cell value:", _threeColorScale);
            
            //Table 5
            _timePeriiodRule = new TimePeriodConditionalFormattingRule(TimePeriod.NextWeek);
            _timePeriiodRule.BackColor = Color.FromArgb(204, 204, 255);
            SetTableAndFormats(16, 1, 4, 6, "Display blue background if cell value is in next week:", _timePeriiodRule, true);
            
            //Table 6
            _textFormattingRule = new TextConditionalFormattingRule(TextConditionOperator.Contains, "O");
            _textFormattingRule.ForeColor = Color.Red;
            _textFormattingRule.FontStyle = new SpreadFontStyle(RegularBoldItalicFontStyle.Bold);
            SetTableAndFormats(16, 8, 4, 6, "Display red foreground if cell value contains \"o\":", _textFormattingRule, false, true);
            
            //Table 7
            _dataBarRule = new DatabarConditionalFormattingRule();
            _dataBarRule.FillColor = Color.Green;
            _dataBarRule.Gradient = true;
            SetTableAndFormats(22, 1, 4, 6, "Display a colored data bar representing cell value:", _dataBarRule);

            //Table 8
            _iconSetRule = new IconSetConditionalFormattingRule(ConditionalFormattingIconSetStyle.FiveColoredArrows);
            SetTableAndFormats(22, 8, 4, 6, "Displays an icon represents cell value:", _iconSetRule);            
        }
        private void SetTableAndFormats(int row, int col, int rowCount, int colCount, string title, IConditionalFormattingRule formatRule, bool IsDateTime=false, bool IsString=false)
        {
            SetTitle(row, col, 1, colCount, title);
            _sheetView.Cells[row, col, row + rowCount - 1, col + colCount - 1].Border = _bdrLine;

            if (IsDateTime)
            {
                var value = DateTime.Now;
                int day = 0;
                for (int r = row + 1; r < row + rowCount; r++)
                {
                    for (int c = col; c < col + colCount; c++)
                    {
                        _sheetView.Cells[r, c].Value = value.AddDays(day++).ToShortDateString();
                    }
                }
            }
            else if (IsString)
            {
                var value = "The quick brown fox jumps over the lazy dog".Split(' ');
                int valueAt = 0;
                for (int r = row + 1; r < row + rowCount; r++)
                {
                    for (int c = col; c < col + colCount; c++)
                    {
                        _sheetView.Cells[r, c].Value = value[valueAt];
                        valueAt++;
                        valueAt = valueAt % value.Length;
                    }
                }
            }
            else
            {
                var value = 0;
                for(int r=row+1;r<row+rowCount;r++)
                {
                    for(int c=col;c<col+colCount;c++)
                    {
                        _sheetView.Cells[r, c].Value = value;
                        value += 10;
                    }
                }
            }
            _sheetView.SetConditionalFormatting(row + 1, col, rowCount - 1, colCount, formatRule);
        }
        private void SetTitle(int row, int col, int rowCount, int colCount, string title)
        {
            _sheetView.Cells[row, col].RowSpan = rowCount;
            _sheetView.Cells[row, col].ColumnSpan = colCount;
            _sheetView.Cells[row, col].BackColor = _bclrHeader;
            _sheetView.Cells[row, col].ForeColor = _fclrHeader;
            _sheetView.Cells[row, col].Font = _fntHeader;
            _sheetView.Cells[row, col].Value = title;
        }        
    }
}


//
//Introduction.Designer.cs
//


namespace Spread.ConditionalFormat
{
    partial class Introduction
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread();
            this._sheetView = new FarPoint.Win.Spread.SheetView();
            this.pnlMain.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this._sheetView)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread1);
            // 
            // fpSpread1
            // 
            this.fpSpread1.AccessibleDescription = "";
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this._sheetView});
            this.fpSpread1.Size = new System.Drawing.Size(477, 487);
            this.fpSpread1.TabIndex = 0;
            // 
            // _sheetView
            // 
            this._sheetView.Reset();
            this._sheetView.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this._sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this._sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // Introduction
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "Introduction";
            this.pnlMain.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this._sheetView)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread1;
        private FarPoint.Win.Spread.SheetView _sheetView;
    }
}
